/**
* @desc desc
* @author 钱炜亮
* @date 2024-04-21
*/
<template>
  <common-page>
    <view class="page-box-info padding-top-40">
			<view class="page-header flex-col align-center padding-bottom-36">
				<image :src="formData.avatar || '../../static/avatar_bg.jpg'" mode="" style="width: 111rpx;height: 111rpx;border: 1px solid #E3E3E3;border-radius: 50%;"></image>
				<view class="flex margin-top-16" @click="bindUploadImage">
					<text class="color-333 font-30">修改头像</text>
					<u-icon name="edit-pen"></u-icon>
				</view>
			</view>
			<view class="info-wrap">
				<u--form
					labelPosition="left"
					labelWidth="160rpx"
					:model="formData"
					:rules="rules"
					ref="uForm"
				>
					<u-form-item
						label="用户名"
						prop="username"
						borderBottom
						required
					>
						<u--input
							v-model="formData.username"
							border="none"
							placeholder="请填写用户名"
							placeholderClass="inputPlaceholder"
						></u--input>
					</u-form-item>
					<u-form-item
						label="所在企业"
						prop="companyName"
						borderBottom
						v-if="formData.userType == 2"
					>
						<u--input
							v-model="formData.companyName"
							border="none"
							readonly
							placeholder="请填写所在企业"
							placeholderClass="inputPlaceholder"
						></u--input>
					</u-form-item>
					<u-form-item
						label="手机号"
						prop="phone"
						required
						borderBottom
					>
						<u--input
							v-model="formData.phone"
							border="none"
							placeholder="请填写手机号"
							placeholderClass="inputPlaceholder"
						></u--input>
					</u-form-item>
					<u-form-item
						label="住址"
						prop="address"
						borderBottom
					>
						<u--textarea
							v-model="formData.address"
							border="none"
							placeholder="请填写住址"
							count
							maxlength="100"
							placeholderClass="inputPlaceholder"
						></u--textarea>
					</u-form-item>
				</u--form>
			</view>
    </view>
		<u-button text="保存" type="primary" color="#417CE9" class="btn-save" @click="bindSave"></u-button>
  </common-page>
</template>

<script>
export default {
	data() {
		return {
			formData: {
				avatar: '',
				username: '',
				companyName: '',
				phone: '',
				address: '',
				userType: '',
				userId: ''
			},
			rules: {
				'username': [{
					type: 'string',
					required: true,
					message: '请填写用户名',
					trigger: ['blur', 'change']
				}, {
					type: 'string',
					max: 20,
					message: '用户名称过长',
					trigger: ['blur']
				}],
				'phone': [{
					type: 'string',
					required: true,
					message: '请填写手机号',
					trigger: ['blur', 'change']
				}, {
					pattern: /^1[3-9]\d{9}$/,
					message: '请填写正确手机号格式',
					trigger: ['blur']
				}],
				'address': {
					type: 'string',
					max: 100,
					message: '字符数小于等于100',
					trigger: ['blur']
				}
			}
		};
	},
	onLoad() {
		this.getUserInfoData();
	},
	methods: {
		// 获取用户信息
		async getUserInfoData() {
			const [err, result] = await this.$to(uni.$u.http.get('/jeecg-boot/api/v1-public/mini-program-user/userInfo'));
			if (err) return;
			this.formData.avatar = result.avatar;
			this.formData.username = result.username;
			this.formData.companyName = result.companyName;
			this.formData.phone = result.phone;
			this.formData.address = result.address;
			this.formData.userType = result.userType || 1;
			this.formData.userId = result.userId;
		},
		// 上传图片
		bindUploadImage() {
			uni.chooseImage({
				count: 1,
				success: async(res) => {
					const [err, result] = await this.$to(uni.$u.http.upload('/jeecg-boot/sys/common/upload', {
						filePath: res.tempFilePaths[0],
						name: 'file'
					}));
					this.formData.avatar = result || err.data.message;
				}
			});
		},
		// 保存
		bindSave() {
			this.$refs.uForm.validate().then(async(res) => {
				const [err] = await this.$to(uni.$u.http.post('/jeecg-boot/api/v1-public/mini-program-company/updateCompanyAppInfo', this.formData));
				if (err) return;
				uni.$u.toast('修改成功');
				uni.$emit('bindUpdateInfo');
				setTimeout(() => {
					uni.navigateBack();
				}, 1500);
			}).catch(errors => {
				uni.$u.toast('校验失败');
			});
		}
		/* ====== S 事件区域 ====== */

		/* ====== E 事件区域 ====== */
		/* ====== S 通用逻辑 ====== */

		/* ====== E 通用逻辑 ====== */
		/* ====== S 接口定义 ====== */

		/* ====== E 接口定义 ====== */
	}
};
</script>

<style>

</style>
<style lang="scss" scoped>
.page-box-info {
	background-color: #fff;
	border-radius: 20rpx;
	margin-top: 32rpx;
	padding-bottom: 20rpx;
	.page-header {
		border-bottom: 1px solid #E3E3E3;
	}
	.info-wrap {
		padding: 0 40rpx;
		.item {
			height: 120rpx;
			border-bottom: 1px solid #E3E3E3;
			&:nth-last-of-type(1) {
				border-bottom: none;
			}
		}
		.txt {
			color: #5B5B5B;
		}
		.val {
			color: #1E2531;
			font-weight: bold;
		}
	}
}

::v-deep .u-button {
	margin-top: 60rpx;
	border-radius: 80rpx !important;
	.u-button__text {
		font-size: 32rpx !important;
		font-weight: bold;
	}
}
</style>
